<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AdminAction
 *
 * @author Administrator
 */
import('@.Service.NewsService');
import('@.Filter.SecurityAction');
import('@.Util.ConstantUtil');
import('ORG.Util.Page');
import("@.ORG.Net.Keditor");

class MessageAction extends SecurityAction {

    function news() {
        $newsService = new NewsService();
        $count = $newsService->countMessages();

        $page = new Page($count, 10);

        $show = $page->show();
        $list = $newsService->listMessages($page->firstRow, $page->listRows, 'datetime desc');

        $this->assign('show', $show);
        $this->assign('list', $list);
        $this->assign('content', 'news');
        $this->display('Admin:global');
    }

    function newsAddPage() {
        $ke = new Keditor();
        $ke->id = "news_content"; //指定textarea的id
        $ke->form = "frm_news_add"; //指定form的id，默认为from1
        $ke->afterCreate = "ctrlenter"; //定义创建后执行函数，ctrlenter表示ctrl+enter提交表单。
        $ke->imageUploadJson="http://www.baipaopao.com/index.php/File/kindeditorUpload.shtml";
        $keshow = $ke->show(); //生成js代码
        $this->assign("keshow", $keshow);

        $id = $_REQUEST['id'];
        if (!empty($id)) {
            $newsService = new NewsService();
            $news = $newsService->findMessageById($id);
//            dump($news);return;
            $this->assign('news', $news);
        }

        $this->assign('content', 'newsAdd');
        $this->display('Admin:global');
    }

    function newsAdd() {
        $newsService = new NewsService();

        $news = $newsService->create();
        $news['datetime'] = date('Y年m月d日');

//        $html = str_get_html($news['content_text']);
//        dump($html->innertext());
        //$news['content'] = $news['content_text'];
        $news['author'] = '白泡泡';
        //$news['icon'] = $_POST['icon'];
        
//        $html = str_get_html($news['content_text']);
        $news['content'] = strip_tags($news['content_text']);
//        dump(strip_tags($news['content_text']));return;
        $newsService->insert($news);
//        dump($news);
        $this->redirect('news');
    }

    function delete() {
        $id = $_REQUEST['id'];
        if (!empty($id)) {
            $newsService = new NewsService();

            $newsService->delete($id);
        }

        $this->redirect('news');
    }

    function floatRed() {
        $id = $_POST['id'];

        if (empty($id)) {
            $this->ajaxReturn(null, ConstantUtil::$MOBILE_PARAMS_NONE, 0);
            return;
        }

        //清除缓存
        F(ConstantUtil::$NEWS_INDEX, NULL);

        $newsService = new NewsService();
        $message = $newsService->findMessageById($id);

        $message['float_red'] = $_POST['float_red'];
//dump($message);
        $newsService->update($message);

        $this->ajaxReturn(null);
    }

}

?>
